package com.gcc.crudjpah2demo.service;

import com.gcc.crudjpah2demo.model.Product;
import com.gcc.crudjpah2demo.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ProductService {
    @Autowired
    private ProductRepository productRepository;

    public Product save(Product product) {
        return productRepository.save(product);
    }

    public List<Product> findAll() {
        return productRepository.findAll();
    }

    public Optional<Product> findById(Long id) {
        return productRepository.findById(id);
    }

    public boolean deleteById(Long id) {
       if (productRepository.existsById(id)){
           productRepository.deleteById(id);
           return true;
       }
       return false;
    }
}
